package com.ls.lserp.modules.setting.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.CollectionsUtils;
import com.ls.lserp.modules.common.entity.Material;
import com.ls.lserp.modules.common.entity.UploadFile;
import com.ls.lserp.modules.setting.dao.MaterialDao;
import com.ls.lserp.modules.setting.entity.Ognization;
import com.ls.lserp.modules.sys.dao.BrandDao;
import com.ls.lserp.modules.sys.dao.UploadFileDao;
import com.ls.lserp.modules.sys.entity.Brand;



/**
  * @ClassName: materialService
  * @Description: 物料 保存 修改 删除
  * 
  * @author 
  * @date 2017年1月19日 上午9:55:34
  *
  */

@Service
@Transactional(readOnly = true)
public class MaterialService extends CrudService<MaterialDao, Material> {
	@Autowired
	
	// 我的物理方法接口
	private MaterialDao materialDao;
	
	
	@Autowired
	private UploadFileDao  uploadFileDao;
	
    /**
      * @Title: findList
      * @Description: 查询我的物料
      * @param @param page
      * @param @param material
      * @param @return
      * @return Page<Material> 
      * @throws
      */
    
    public Page<Material> findList(Page<Material> page, Material material) {
        // 设置分页参数
    	material.setPage(page);
        // 执行分页查询
        page.setList(materialDao.findList(material));
        
        return page;
    }
    
	/**
	  * @Title: getMaterial
	  * @Description: 获得我的物料信息
	  * @param @param id
	  * @param @return
	  * @return Material 
	  * @throws
	  */
	
	public Material getMaterial(Long id){
	       // 实力化实体类
		   Material material=new Material();
	       
		   // 执行查询语句，获取基本信息
		   material=materialDao.get(id);
	        
	        return material;
	    }



	
	/**
	  * @Title: saveMaterial
	  * @Description: 保存更新我的物料
	  * @param @param material
	  * @return void 
	  * @throws
	  */
	
	@Transactional(readOnly = false)
	public void saveMaterial(Material material) {
		
		// 保存图片
		if(material.getUploadFile()!=null){
			
		    UploadFile uploadFile=material.getUploadFile();
        	
		    if(!CollectionsUtils.isEmpty(uploadFileDao.searchFileId(uploadFile))){
        		
        		// 返回查询结果id
		        material.setFileId(uploadFile.getFileId());
        	}else{
        	    uploadFile.preInsert();
        		// 若为null 执行保存操作
        	    uploadFile.setFileKbn(0);
        	    materialDao.saveUploadFile(uploadFile);
        		
        		material.setFileId(uploadFile.getFileId());
        		
        	}
		}
		
		// 判断新建 编辑
		// 若为null时执行插入操作（新建）
		if (material.getId()==null){
			material.preInsert();
			materialDao.insert(material);
			materialDao.insertVenderMaterial(material);
			materialDao.insertiInventoryMaterial(material);
		// 编辑
		}else{
			// 更新数据
			material.preUpdate();
			materialDao.update(material);
			materialDao.updateVenderMaterial(material);
		}
		
		
	}

	/**
	  * @Title: deleteMaterial
	  * @Description: 删除我的物料
	  * @param @param material
	  * @return void 
	  * @throws
	  */
	
	@Transactional(readOnly = false)
	public void deleteMaterial(Material material) {
		
		// 逻辑删除
		materialDao.delete(material);
	}
	
	/**
	  * @Title: getByName
	  * @Description: 查重
	  * @param @param materialNameCn
	  * @param @return
	  * @return Material 
	  * @throws
	  */
	
	public Material getByCode(String Code) {
		
		// 实例化实体类
		Material material = new Material();
		material.setMaterialCode(Code);
		
		// 执行查询返回查询结果
		return materialDao.getByCode(material);
	}
	
	/**
	 * @Title: findMaterialOgnizationList
	 * @Description: 来源机构一览
	 * @return List<Ognization>
	 */
	public List<Ognization> findMaterialOgnizationList(Material material) {
		material.setCurrentUser(material.getCurrentUser());
		return materialDao.findMaterialOgnizationList(material);
	}
	/**
     * @Title: selBrandList
     * @Description: 查询
     * @param @return
     * @return List<Brand>
     * @throws
     */
   public List<Brand> selBrandList(Material material) {
   
	   return materialDao.findBrandList();
   }
   /**
    * @Title: selBrandList
    * @Description: 查询
    * @param @return
    * @return Ognization
    * @throws
    */
  public Ognization getMaterialOgnization(Material material){
	  Ognization ognization = materialDao.getMaterialOgnization(material);
	  List<Brand> brandList = materialDao.findBrandList();
	  if (ognization!=null) {
		  ognization.setSelBrandList(brandList);
	  }
	   return ognization;
  }
}
